package yx.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsCorporation;
import yx.model.SmCode;
import yx.vo.CorporationVO;
import yx.vo.DatagridVO;

@Service("corporationService")
public class CorporationServiceImpl implements CorporationServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CorporationServiceImpl.class);

	private BaseDaoInt<BsCorporation> corporationDao;
	private BaseDaoInt<SmCode> codeDao;

	public BaseDaoInt<BsCorporation> getCorporationDao() {
		return corporationDao;
	}

	@Autowired
	public void setCorporationDao(BaseDaoInt<BsCorporation> corporationDao) {
		this.corporationDao = corporationDao;
	}

	public BaseDaoInt<SmCode> getCodeDao() {
		return codeDao;
	}

	@Autowired
	public void setCodeDao(BaseDaoInt<SmCode> codeDao) {
		this.codeDao = codeDao;
	}

	@Override
	public CorporationVO save(CorporationVO corporation) {
		BsCorporation t = new BsCorporation();
		BeanUtils.copyProperties(corporation, t);
		corporationDao.save(t);
		BeanUtils.copyProperties(t, corporation);
		// 获得企业类型的名称
		if (corporation.getCorporationType() != null && !"".equals(corporation.getCorporationType())) {
			Map<String, String> corpTypeMap = getCorpType("01");
			corporation.setCorporationTypeName(corpTypeMap.get(corporation.getCorporationType()));
		}
		return corporation;
	}

	// 获得企业类型
	private Map<String, String> getCorpType(String type) {
		Map<String, String> retMap = new HashMap<String, String>();
		if (type != null && !"".equals(type)) {
			String hql = "from SmCode t where t.ctype = :ctype";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("ctype", type);
			List<SmCode> l = codeDao.find(hql, params);
			for (SmCode c : l) {
				retMap.put(c.getCvalue(), c.getCname());
			}
		}
		return retMap;
	}

	@Override
	public DatagridVO datagrid(CorporationVO corporation) {
		DatagridVO dg = new DatagridVO();
		String hql = "from BsCorporation t where 1 = 1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(corporation, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(corporation, hql);
		List<BsCorporation> l = corporationDao.find(hql, params, corporation.getPage(), corporation.getRows());
		List<CorporationVO> nl = new ArrayList<CorporationVO>();
		changeModel(l, nl);
		dg.setTotal(corporationDao.count(totalHql, params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List<BsCorporation> l, List<CorporationVO> nl) {
		if (l != null && l.size() > 0) {
			// 获得企业类型的名称
			Map<String, String> corpTypeMap = getCorpType("01");
			for (BsCorporation t : l) {
				CorporationVO u = new CorporationVO();
				BeanUtils.copyProperties(t, u);
				if (t.getCorporationType() != null && !"".equals(t.getCorporationType())) {
					u.setCorporationTypeName(corpTypeMap.get(t.getCorporationType()));
				}
				nl.add(u);
			}
		}
	}

	private String addOrder(CorporationVO corporation, String hql) {
		if (corporation.getSort() != null || corporation.getOrder() != null) {
			hql += " order by " + corporation.getSort() + " " + corporation.getOrder();
		}
		return hql;
	}

	private String addWhere(CorporationVO corporation, String hql, Map<String, Object> params) {
		if (corporation.getCorporationName() != null && !"".equals(corporation.getCorporationName().trim())) {
			hql += " and t.corporationName like :corporationName";
			params.put("corporationName", "%%" + corporation.getCorporationName().trim() + "%%");
		}
		if (corporation.getRegion() != null && !"".equals(corporation.getRegion().trim())) {
			hql += " and t.region like :region";
			params.put("region", "%%" + corporation.getRegion().trim() + "%%");
		}
		if (corporation.getLegalPerson() != null && !"".equals(corporation.getLegalPerson().trim())) {
			hql += " and t.legalPerson like :legalPerson";
			params.put("legalPerson", "%%" + corporation.getLegalPerson().trim() + "%%");
		}
		if (corporation.getContactPerson() != null && !"".equals(corporation.getContactPerson().trim())) {
			hql += " and t.contactPerson like :contactPerson";
			params.put("contactPerson", "%%" + corporation.getContactPerson().trim() + "%%");
		}
		return hql;
	}

	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete BsCorporation t where t.id in (";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		corporationDao.executeHql(hql);
	}

	@Override
	public CorporationVO edit(CorporationVO corporation) {
		BsCorporation t = corporationDao.get(BsCorporation.class, corporation.getId());
		BeanUtils.copyProperties(corporation, t);
		if (corporation.getCorporationType() != null && !"".equals(corporation.getCorporationType())) {
			Map<String, String> corpTypeMap = getCorpType("01");
			corporation.setCorporationTypeName(corpTypeMap.get(corporation.getCorporationType()));
		}
		return corporation;
	}

}
